package com.jec.module.sysconfig.service.convertor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jec.module.sysconfig.dao.TonglingRepository;
import com.jec.module.sysconfig.entity.TonglingConfig;
import com.jec.module.sysmonitor.dao.NetUnitRepository;
import com.jec.utils.Response;

/**
 * Created by jeremyliu on 9/26/16.
 */
@Service
public class TonglingConvertor implements Convertor<TonglingConfig> {

    @Autowired
    private NetUnitRepository netUnitRepository;

    @Autowired
    private TonglingRepository tonglingRepository;

    @Override
    public Response process(TonglingConfig tonglingConfig) {
        Response response = Response.Builder().status(Response.STATUS_PARAM_ERROR);

        if(netUnitRepository.findOne(tonglingConfig.getNetunit()) == null)
            return response.message("网元不存在");
        if(tonglingRepository.existsByNetunitAndCodeAndIdNot(
        		tonglingConfig.getNetunit(), 
        		tonglingConfig.getCode(), 
        		tonglingConfig.getId()))
            return response.message("该网元编码为"+tonglingConfig.getCode()+"已配置");

        return response.status(Response.STATUS_SUCCESS);
    }

    @Override
    public Response checkBeforeRemove(int id) {
        return null;
    }
}
